package net.zoneland.geological.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.zoneland.geological.project.entity.Tbl_project;
import net.zoneland.geological.project.entity.Tbl_zht;
import net.zoneland.geological.project.service.Tbl_projectServiceI;
import net.zoneland.geological.project.service.Tbl_zhtServiceI;
import net.zoneland.geological.system.entity.ZTreeEntity;
import net.zoneland.geological.usermanage.entity.Tbl_user;
import net.zoneland.geological.usermanage.service.Tbl_user_datapermissionServiceI;
import net.zoneland.greenleaf.system.controller.BaseController;

/**
 * 区域信息表操作控制层(Controller|Action)

 * 表   名：TBL_AREACONTROLLER<br/>
 * 注   释：区域信息表<br/>
 * 作   者：李义<br/>
 * 日   期：2017-05-28 23:06:48
**/
@Controller
@RequestMapping("ztree")
public class Tbl_ztreeController extends BaseController { 

	/**
	 * 如果需要系统对此Controller的访问进行监控，记录服务访问的时间，耗时，那么请将monitor属性设置为“true”
	 */

	public boolean monitor = false; 

	private static Logger logger = Logger.getLogger(Tbl_ztreeController.class);

	//获取类的名称
	@SuppressWarnings("unused")
	private String clazzName = new Object() {
		public String getClassName() {
			String clazzName = this.getClass().getName();
			return clazzName.substring(0, clazzName.lastIndexOf('$'));
		}
	}.getClassName();

	/**
	 * tbl_areaService
	 * @param tbl_areaService
	**/	
	@Autowired
	private Tbl_zhtServiceI tbl_zhtService;
	
	@Autowired
	private Tbl_projectServiceI tbl_projectService;
	
	@Autowired
	private Tbl_user_datapermissionServiceI tbl_user_datapermissionService;
	
	/**
	 * 获取区域信息表列表
	 * 返回JSON格式数据，如果不需要分页，您可以考虑将pagesize设置为足够大的数字，系统默认为20
	 * @param Map<String, Object> paramMap
	 * @throws Exception 
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/city" )
	@ResponseBody
	public Object listByCity( @RequestBody Map<String, Object> paramMap ) throws Exception {
		Map<String, Object> map = new HashMap<String,Object>();
		List<ZTreeEntity> zNodes = new ArrayList<>();
		List<ZTreeEntity> project_entities = new ArrayList<>();
		List<ZTreeEntity> zht_entities = new ArrayList<>();
		
		ZTreeEntity city_entity = null;
		ZTreeEntity project_entity = null;
		ZTreeEntity zht_entity = null;
		
		List<Tbl_project> projectList = null;
		List<Tbl_zht> zhtList = null;
		
		List<String> projectIds = null;
		List<String> zhtIds = null;
		Tbl_user tbl_user = null;
		Integer maxLevel = 1;
		String city_code = null;
		String city_name = null;
		Boolean check = true;
		if( paramMap == null ){
			check = false;
			map.put( "result", "error");
			map.put( "message", "参数列表为空，无法进行数据查询!");
		}
		
		if( check ){
			if( this.getSession().getAttribute( "login_user" ) != null ) {
				tbl_user = (Tbl_user)this.getSession().getAttribute( "login_user" );
			}else {
				check = false;
				map.put( "result", "error" );
				map.put( "login", false );
				map.put( "message", "用户未登录!" );
			}
		}	
		
		if( check ){
			if( paramMap.get( "city_code" ) == null ){
				check = false;
				map.put( "result", "error");
				map.put( "message", "参数city_code为空，无法进行数据查询!");
			}
			if( paramMap.get( "city_name" ) == null ){
				check = false;
				map.put( "result", "error");
				map.put( "message", "参数city_name为空，无法进行数据查询!");
			}
			if( paramMap.get( "maxLevel" ) == null ){
				maxLevel = 1;
			}
		}
		if( check ){
			city_code = paramMap.get( "city_code" ).toString();
			city_name = paramMap.get( "city_name" ).toString();
			maxLevel = Integer.parseInt( paramMap.get( "maxLevel" ).toString() );
			city_entity = new ZTreeEntity();
			city_entity.setId( city_code );
			city_entity.setName( city_name );
			city_entity.setIsParent( true );
			city_entity.setLevel( 1 );
			city_entity.setIconSkin( "myTree-level1" );
			city_entity.setOpen( true );
		}
		
		if( check ){
			paramMap.clear();
			//需要查询项目，根据用户数据权限，对可见的项目进行过滤
			projectIds = tbl_user_datapermissionService.getVisibleProjectIdsWithCityCodeLoginName( city_code, tbl_user.getLogin_name() );
			if( projectIds != null && !projectIds.isEmpty() ) {
				paramMap.put( "ids", projectIds );
			}else {
				paramMap.put( "id", "无" );
			}
		}
		
		if( check ){
			//查询该城市中用户可见的项目
			paramMap.put( "city_code", city_code );
			try {
				projectList = tbl_projectService.getList( paramMap );
			} catch (Exception e) {
				check = false;
				map.put( "result", "error");
				map.put( "message", "根据城市编码查询项目信息列表时发生异常!");
				logger.error( "根据城市编码查询项目信息列表时发生异常!", e );
			}
		}
		
		if( check ){
			if( projectList != null && !projectList.isEmpty() ){
				int i = 0;
				for( Tbl_project project : projectList ){
					i++;
					project_entity = new ZTreeEntity();
					project_entity.setId( project.getId() );
					project_entity.setName( project.getDescription() );
					project_entity.setP_code( city_code );
					project_entity.setLevel( 2 );
					project_entity.setIconSkin( "myTree-level2" );
					if( maxLevel > 2 ){
						project_entity.setIsParent( true );
						if( i == 1 ){
							project_entity.setOpen( true );
							
							//需要查询项目，根据用户数据权限，对可见的灾害体进行过滤
							zhtIds = tbl_user_datapermissionService.getZhtIdsWithPermission( tbl_user.getLogin_name() );
							
							paramMap.clear();
							paramMap.put( "project_id", project.getId() );
							//获取数据权限对灾害体的过滤
							if( zhtIds != null && !zhtIds.isEmpty() ) {
								paramMap.put( "ids", zhtIds );
							}else {
								paramMap.put( "id", "无" );
							}
							
							try {
								zhtList = tbl_zhtService.getList( paramMap );
								if( zhtList != null && !zhtList.isEmpty() ){
									project_entity.setIsParent( true );
									for( Tbl_zht tbl_zht : zhtList ){
										zht_entity = new ZTreeEntity();
										zht_entity.setId( tbl_zht.getId() );
										zht_entity.setName( tbl_zht.getName() );
										zht_entity.setIsParent( false );
										zht_entity.setP_code( project.getId() );
										zht_entity.setLevel( 3 );
										zht_entity.setIconSkin( "myTree-level3" );
										zht_entities.add( zht_entity );
									}
									project_entity.setChildren( zht_entities );
								}
							} catch (Exception e) {
								check = false;
								map.put( "result", "error");
								map.put( "message", "根据项目ID查询灾害体信息列表时发生异常!");
								logger.error( "根据项目ID查询灾害体信息列表时发生异常!", e );
							}
						}else{
							project_entity.setOpen( false );
							paramMap.clear();
							paramMap.put( "project_id", project.getId() );
							int count = 0;
							try {
								count = tbl_zhtService.getListCount( paramMap );
								if( count > 0 ){
									project_entity.setIsParent( true );
								}else{
									project_entity.setIsParent( false );
								}
							} catch (Exception e) {
								check = false;
								map.put( "result", "error");
								map.put( "message", "根据项目ID查询灾害体信息列表时发生异常!");
								logger.error( "根据项目ID查询灾害体信息列表时发生异常!", e );
							}
						}
					}else{
						project_entity.setIsParent( false );
						project_entity.setOpen( false );
					}
					project_entities.add( project_entity );
				}
				city_entity.setChildren( project_entities );
			}
		}
		
		if( check ){
			zNodes.add( city_entity );
		}
		if( check ){
			map.put( "result", "success" );
			map.put( "message", "系统查询信息数据成功!" );
			map.put( "data", zNodes );
		}
		return map;
	}
	
	/**
	 * 以树状结构来展示地区结构
	 * 
	 * @param Map<String, Object> paramMap
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/children" )
	@ResponseBody
	public Object listChildren( @RequestBody Map<String, Object> paramMap ) {
		Map<String, Object> map=new HashMap<String,Object>();
		List<ZTreeEntity> list  = null;
		Boolean check = true;
		
		if( check ){
			map.put( "result", "success" );
			map.put( "message", "系统查询信息数据成功!" );
			map.put( "data", list );
		}
		return map;
	}
}
